package cn.wolfcode.job;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.fegin.SeckillFeginApi;
import cn.wolfcode.redis.CommonRedisKey;
import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**

 * 用于处理用户缓存的定时任务
 * 为了保证Redis中的内存的有效使用。
 * 我们默认保留7天内的用户缓存数据，每天凌晨的时候会把7天前的用户登录缓存数据删除掉
 */
@Component
@Setter@Getter
@RefreshScope
@Slf4j
public class SeckillProductUpJob implements SimpleJob {
    @Value("${jobCron.SeckillProductUpJob}")
    private String cron;
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Qualifier("cn.wolfcode.fegin.SeckillFeginApi")
    @Autowired
    private SeckillFeginApi seckillFeginApi;
    @Override
    public void execute(ShardingContext shardingContext) {
        doWork();
    }
    private void doWork() {
    
    log.info("秒杀商品上架{}",new Date());

        Integer[] times={10,12,14};
        List<String> integers = new ArrayList<>();
        List<String> stoey = new ArrayList<>();
        for (Integer time : times) {
            String key = CommonRedisKey.SECKILL_PRODUCT.getRealKey(String.valueOf(time));
            String stoeykey = CommonRedisKey.SECKILL_PRODUCT_STORE.getRealKey(String.valueOf(time));
            integers.add(key);
            stoey.add(stoeykey);
        }
        //清楚旧数据
        redisTemplate.delete(integers);
        //清除redis中秒杀商品的信息
        redisTemplate.delete(stoey);
 
        
        
        //调用秒杀服务 获取秒杀商品列表
     Result<List<SeckillProductVo>> result= seckillFeginApi.querySeckillProduct();
      if (result.hasError()|| StringUtils.isEmpty(result)){
         log.info(String.valueOf(CommonCodeMsg.NO_SECKILL_PRODUCT));
          throw new BusinessException(CommonCodeMsg.NO_SECKILL_PRODUCT);
      }
    //把商品放入redis
        for (SeckillProductVo vo : result.getData()) {
            String bigkey = CommonRedisKey.SECKILL_PRODUCT.getRealKey(String.valueOf(vo.getTime()));
            String bigkeystory = CommonRedisKey.SECKILL_PRODUCT_STORE.getRealKey(String.valueOf(vo.getTime()));
            
            
            Long id = vo.getId();
            //存秒杀数据
            redisTemplate.opsForHash().put(bigkey, String.valueOf(id), JSON.toJSONString(vo));
            //存秒杀数量
            redisTemplate.opsForHash().put(bigkeystory, String.valueOf(id),String.valueOf(vo.getStockCount()));
        }
        
       
        //把秒杀商品的数量放入redis中
        
        
        log.info("秒杀商品上架成功{}",new Date());
    }


}
